import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class SettingsPage extends StatefulWidget {
  final double height;
  final double weight;
  final String gender;
  final DateTime? birthDate;

  const SettingsPage({
    super.key,
    required this.height,
    required this.weight,
    required this.gender,
    required this.birthDate,
  });

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  late double height;
  late double weight;
  late String gender;
  DateTime? birthDate;

  final TextEditingController heightController = TextEditingController();
  final TextEditingController weightController = TextEditingController();

  @override
  void initState() {
    super.initState();
    height = widget.height;
    weight = widget.weight;
    gender = widget.gender;
    birthDate = widget.birthDate;

    heightController.text = height.toString();
    weightController.text = weight.toString();
  }

  Future<void> _selectBirthDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
      context: context,
      initialDate: birthDate ?? DateTime(2000),
      firstDate: DateTime(1900),
      lastDate: DateTime.now(),
    );
    if (picked != null && picked != birthDate) {
      setState(() {
        birthDate = picked;
      });
    }
  }

  void _saveSettings() {
    Navigator.pop(context, {
      'height': double.tryParse(heightController.text) ?? height,
      'weight': double.tryParse(weightController.text) ?? weight,
      'gender': gender,
      'birthDate': birthDate,
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置个人信息'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: heightController,
              keyboardType: TextInputType.number,
              decoration: const InputDecoration(labelText: '输入身高 (cm)'),
            ),
            TextField(
              controller: weightController,
              keyboardType: TextInputType.number,
              decoration: const InputDecoration(labelText: '输入体重 (kg)'),
            ),
            const SizedBox(height: 16),
            Row(
              children: [
                const Text('选择性别:'),
                const SizedBox(width: 16),
                DropdownButton<String>(
                  value: gender,
                  items: const [
                    DropdownMenuItem(value: '男性', child: Text('男性')),
                    DropdownMenuItem(value: '女性', child: Text('女性')),
                  ],
                  onChanged: (String? newValue) {
                    setState(() {
                      gender = newValue!;
                    });
                  },
                ),
              ],
            ),
            const SizedBox(height: 16),
            Row(
              children: [
                const Text('出生日期:'),
                const SizedBox(width: 16),
                Text(birthDate == null
                    ? '未选择'
                    : DateFormat('yyyy-MM-dd').format(birthDate!)),
                const SizedBox(width: 16),
                ElevatedButton(
                  onPressed: () => _selectBirthDate(context),
                  child: const Text('选择日期'),
                ),
              ],
            ),
            const SizedBox(height: 32),
            ElevatedButton(
              onPressed: _saveSettings,
              child: const Text('保存'),
            ),
          ],
        ),
      ),
    );
  }
}